package com.yxp.core.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.yxp.core.dao.UserDao;
import com.yxp.core.entity.User;
import com.yxp.core.util.DBConnUtil;

public class UserDaoImpl implements UserDao {
	

	@Override
	public boolean login(String name, String pwd) {
		boolean flag = false;
		try {
			DBConnUtil.init();
			ResultSet rs = DBConnUtil.selectSql("select * from t_user where name='" + name + "' and pwd='" + pwd + "'");
			while (rs.next()) {
				if (rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)) {
					flag = true;
				}
			}
			DBConnUtil.closeConn();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean register(User user) {
		boolean flag = false;
		DBConnUtil.init();
		int i = DBConnUtil.addUpdDel("insert into t_user(name,pwd,sex,home,info) " + "values('" + user.getName() + "','"
				+ user.getPwd() + "','" + user.getSex() + "','" + user.getHome() + "','" + user.getInfo() + "')");
		if (i > 0) {
			flag = true;
		}
		DBConnUtil.closeConn();
		return flag;
	}

	@Override
	public List<User> getUserAll() {
		List<User> list = new ArrayList<User>();
		try {
			DBConnUtil.init();
			ResultSet rs = DBConnUtil.selectSql("select * from t_user");
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPwd(rs.getString("pwd"));
				user.setSex(rs.getString("sex"));
				user.setHome(rs.getString("home"));
				user.setInfo(rs.getString("info"));
				list.add(user);
			}
			DBConnUtil.closeConn();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean delete(int id) {
		boolean flag = false;
		DBConnUtil.init();
		String sql = "delete from t_user where id=" + id;
		int i = DBConnUtil.addUpdDel(sql);
		if (i > 0) {
			flag = true;
		}
		DBConnUtil.closeConn();
		return flag;
	}

	@Override
	public boolean update(int id, String name, String pwd, String sex, String home, String info) {
		boolean flag = false;
		DBConnUtil.init();
		String sql = "update t_user set name ='" + name + "' , pwd ='" + pwd + "' , sex ='" + sex + "' , home ='" + home
				+ "' , info ='" + info + "' where id = " + id;
		int i = DBConnUtil.addUpdDel(sql);
		if (i > 0) {
			flag = true;
		}
		DBConnUtil.closeConn();
		return flag;
	}

}
